﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using WeifenLuo.WinFormsUI.Docking;

namespace CreateEntity
{
    public partial class Main : Form
    {
        MainResource resource;//主窗口中的对资源
        public Main()
        {
            InitializeComponent();
            resource = new MainResource(this);
        }

        private void Main_Load(object sender, EventArgs e)
        {
            if (File.Exists("skin\\office2007.ssk"))
            {
                //skinEngine.SkinFile = "skin\\office2007.ssk";
            }
            ShowDataBase();
        }

        private void mItemShowData_Click(object sender, EventArgs e)
        {
            ShowDataBase();
        }

        /// <summary>
        /// 显示数据源
        /// </summary>
        private void ShowDataBase()
        {
            foreach (IDockContent content in dockPanel.Contents)
            {
                DataBaseTree t = content as DataBaseTree;
                if (t is DataBaseTree)
                {
                    t.Show();
                    return;//如果已经打开了数据源。直接退出；
                }
            }
            DataBaseTree tree = new DataBaseTree(resource);
            tree.Show(dockPanel, DockState.DockLeft);
        }

        //将代码复制到剪贴板
        private void toolItemCopy_Click(object sender, EventArgs e)
        {
            Copy();
        }

        /// <summary>
        /// 复制代码
        /// </summary>
        public void Copy()
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                code.richTextBox.SelectAll();
                code.richTextBox.Copy();
                UpdateState("复制完成");
            }
        }

        //复制代码
        private void cMenuItemCopy_Click(object sender, EventArgs e)
        {
            Copy();
        }


        private void cMenuItemSave_Click(object sender, EventArgs e)
        {
            SaveFile();
        }

        /// <summary>
        /// 另存为代码
        /// </summary>
        public void SaveFile()
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.FileName = code.Text;
                saveDialog.Tag = "保存";
                saveDialog.Filter = "C#(*.cs)|*.cs";
                saveDialog.DefaultExt = ".cs";
                if (saveDialog.ShowDialog().Equals(DialogResult.OK))
                {
                    StreamWriter sw = new StreamWriter(saveDialog.FileName, false, Encoding.Unicode);
                    sw.Write(code.richTextBox.Text);
                    sw.Close();
                    UpdateState("保存完成");
                }
            }
        }

        //关闭
        private void cItemMenuCloseTab_Click(object sender, EventArgs e)
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                code.Close();
            }
        }

        private void cMenuItemClose_Click(object sender, EventArgs e)
        {
            EntityCode codeActive = dockPanel.ActiveDocument as EntityCode;
            IList<EntityCode> list = new List<EntityCode>();
            if (codeActive is EntityCode)//判断是否得到活动文档;
            {
                foreach (IDockContent content in dockPanel.Documents)
                {
                    EntityCode code = content as EntityCode;
                    if (code is EntityCode)
                    {
                        if (code != codeActive)
                        {
                            list.Add(code);
                        }
                    }
                }
            }

            //关闭全部
            foreach (EntityCode code in list)
            {
                code.Close();
            }
            UpdateState("关闭完成");
        }

        /// <summary>
        /// 更新状态栏
        /// </summary>
        /// <param name="msg">状态信息</param>
        public void UpdateState(string msg)
        {
            this.tSatusMsg.Text = msg;
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewFile();
        }

        /// <summary>
        /// 新建文件
        /// </summary>
        private void NewFile()
        {
            string fileName = "Code File";
            EntityCode code = new EntityCode(resource);
            int count = 1;//文档名称从1开始
            foreach (IDockContent content in dockPanel.Documents)
            {
                EntityCode codeDocument = content as EntityCode;
                if (codeDocument is EntityCode)
                {
                    if (codeDocument.Text.StartsWith(fileName))
                    {
                        int currentCount = 0;
                        if (Int32.TryParse(codeDocument.Text.Substring(fileName.Length), out currentCount))
                        {
                            count = currentCount + 1;
                        }
                    }
                }
            }
            code.Text = fileName + " " + count;
            code.Show(dockPanel, DockState.Document);
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        /// <summary>
        /// 打开文件
        /// </summary>
        private void OpenFile()
        {
            OpenFileDialog oDialog = new OpenFileDialog();
            oDialog.Title = "打开文件";
            oDialog.Filter = "C#文件(*.cs)|*.cs|所有文件(*.*)|*.*";
            if (oDialog.ShowDialog().Equals(DialogResult.OK))
            {
                EntityCode code = new EntityCode(resource);
                StreamReader stream = new StreamReader(oDialog.FileName);
                code.Text = Path.GetFileNameWithoutExtension(oDialog.FileName);//设置标题名为文件名
                code.richTextBox.Text = stream.ReadToEnd();
                stream.Close();
                code.Show(dockPanel, DockState.Document);
            }
        }

        private void 新建NToolStripButton_Click(object sender, EventArgs e)
        {
            NewFile();
        }

        private void 打开OToolStripButton_Click(object sender, EventArgs e)
        {
            OpenFile();
        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            SaveFile();
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFile();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Copy();
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectAll();
        }

        /// <summary>
        /// 全选
        /// </summary>
        public void SelectAll()
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                code.RichTextBox.SelectAll();
                UpdateState("全选完成");
            }
        }

        private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
        {
            Paste();
        }

        /// <summary>
        /// 粘贴
        /// </summary>
        public void Paste()
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                code.RichTextBox.Paste();
                UpdateState("粘贴完成");
            }
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Paste();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cut();
        }

        /// <summary>
        /// 剪贴
        /// </summary>
        public void Cut()
        {
            EntityCode code = dockPanel.ActiveDocument as EntityCode;
            if (code is EntityCode)
            {
                code.RichTextBox.Cut();
            }
        }

        private void 剪切UToolStripButton_Click(object sender, EventArgs e)
        {
            Cut();
        }

        private void tMenuItemData_Click(object sender, EventArgs e)
        {
            ShowDataBase();
        }

        private void tMenuItemHelp_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://houleixx.cnblogs.com");
        }

        private void tMenuItemContact_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://houleixx.cnblogs.com");
        }

        private void tMenuItemAbout_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }

        private void createSqlHelper_Click(object sender, EventArgs e)
        {
            EntityCode code = new EntityCode(resource);
            code.Text = "SqlHelper";
            using (StreamReader reader = new StreamReader(Application.StartupPath + @"\\Tools\SqlHelper.cs"))
            {
                code.RichTextBox.Text = reader.ReadToEnd();
                reader.Close();
            }
            code.Show(dockPanel, DockState.Document);
            UpdateState("生成SqlHelper完成");
        }
    }
}